
import configs from './config.js'
let token = ''
if (uni.getStorageSync('token')) {
	token = uni.getStorageSync('token');
}
const baseUrl = configs.ApiUrl
const service = (api = '', date = {}, type = 'GET', header = {}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: api.method,
			url: baseUrl + api.url,
			data: api.data,
			header: {
				'Content-Type':'application/x-www-form-urlencoded',
				'token': uni.getStorageSync('token')

			},
			dataType: 'json',
		}).then((response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 500);
			let [error, res] = response;
			if (error == null) {
				if (res.data.code === 1) {
					resolve(res.data);
				} else if(res.data.code === 401) {
					uni.clearStorageSync();
					uni.showModal({
						title: '提示',
						content: '请登录后操作',
						showCancel:false,
						success: function (res) {
							if (res.confirm) {
								uni.navigateTo({
									url:"/pages/index/login"
								})
							} 
						}
					});
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.msg,
						duration: 2000
					});
					reject(res)
					// resolve();
				}
			} else {
				uni.showToast({
					title: '请求超时，请重试',
					icon: 'none', //将值设置为 success 或者 ''
					duration: 2000 //持续时间为 2秒
				})
				reject(err)
			}





		})

	});
}
export default service
